package cn.lijiajia3515.cairo.common;

import cn.lijiajia3515.cairo.auth.CairoAuthMeta;
import cn.lijiajia3515.cairo.auth.modules.account.Account;
import cn.lijiajia3515.cairo.mongo.data.CreatedMongo;
import cn.lijiajia3515.cairo.mongo.data.LastModifiedMongo;
import cn.lijiajia3515.cairo.mongo.data.MetaMongo;

import java.util.Map;
import java.util.Optional;

public class BaseConverter {

	public static CairoAuthMeta convert(MetaMongo metadata, Map<String, Account> accountMap) {
		return CairoAuthMeta.builder()
			.created(Optional.ofNullable(metadata.getCreated()).map(x -> CairoAuthMeta.Action.builder().account(accountMap.get(x.getAccountId())).at(x.getAt()).build()).orElse(null))
			.lastModified(Optional.ofNullable(metadata.getLastModified()).map(x -> CairoAuthMeta.Action.builder().account(accountMap.get(x.getAccountId())).at(x.getAt()).build()).orElse(null))
			.build();
	}

	public static CairoAuthMeta.Action convert(CreatedMongo created, Map<String, Account> accountMap) {
		return CairoAuthMeta.Action.builder().at(created.getAt()).account(accountMap.get(created.getAccountId())).build();
	}

	public static CairoAuthMeta.Action convert(LastModifiedMongo created, Map<String, Account> accountMap) {
		return CairoAuthMeta.Action.builder().at(created.getAt()).account(accountMap.get(created.getAccountId())).build();
	}
}
